<script setup>
import { useRoute, onBeforeRouteUpdate } from 'vue-router';
import { useStore } from 'vuex';
import { useMouseInElement } from '@vueuse/core';
import { ref, computed, watchEffect, nextTick } from 'vue';
import { regionDataPlus } from 'element-china-area-data';

const store = useStore();
const route = useRoute();
const loading = ref(false);
// 商品详细信息
watchEffect(() => {
  loading.value = true;
  store.dispatch('liviinghome/updateProduct', route.params.id);
  const params = {
    id: route.params.id,
    limit: 16,
  };
  store.dispatch('liviinghome/updateSubBanner', params);
  const hot = {
    id: route.params.id,
    limit: 4,
    type: 1,
  };
  // 24热销榜
  store.dispatch('liviinghome/updateSubHotSale', hot);
  const weekhot = {
    id: route.params.id,
    limit: 4,
    type: 2,
  };
  // 周榜
  store.dispatch('liviinghome/updateSubWeekHot', weekhot);
});
// function init(to) {
// store.dispatch('liviinghome/updateProduct', route.params.id);
// }
// init(route);
// onBeforeRouteUpdate(init);
// 24热销榜
const subHotSaleList = computed(() => store.state.liviinghome.subHotSaleList);
// 周热销榜
const subWeekHotList = computed(() => store.state.liviinghome.subWeekHotList);
const productList = computed(() => store.state.liviinghome.productList);
// 轮播图数据
const intervalNum = ref(1000);
const subBanner = computed(() => store.getters['liviinghome/subBanner']);
watch(subBanner, () => {
  setTimeout(() => {
    intervalNum.value = null;
  }, 1000);
});

watch(productList, () => {
  loading.value = false;
});

const target = ref(null);
// isOutside是否进入指定区域 进入为false 否则为true
// elementX 鼠标X位置
// elementY 鼠标Y位置
const { isOutside, elementX, elementY } = useMouseInElement(target); // useMouseInElement(指定的区域)鼠标进入的位置
// const images = ref(productList.value.mainPictures[0]);
const position = computed(()=>{
  let x = elementX.value - 100;
  let y = elementY.value - 100;
  x = x < 0 ? 0 : x;
  y = y < 0 ? 0 : y;
  x = x > 200 ? 200 : x;
  y = y > 200 ? 200 : y;
  return {
    x,
    y,
  };
});
const index = ref(0);
function onEnter(i) {
  index.value = i;
}
// 地区信息
const options = ref(regionDataPlus);
// 选项值
const selectedOptions = ref([
  '110000',
  '110100',
  '110101',
]);


function handleChange(val) {

}

// 选中的规格
const indexPrice = ref(-1);
function onSelect(i) {
  indexPrice.value = i;
}
// 选中的数量
const num = ref(1);
function onNumChange() {

}

const activeName = ref('first');
function handleClick(e) {
  console.log(e);
}
</script>

<template>
  <div class="main">
    <!-- 面包屑 -->
    <div class="crumbs">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <!-- <el-breadcrumb-item :to="{ path: `/livinghome/${ productList.categories[1]?.id }` }">{{ productList.categories[1]?.name }}</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: `/category/sub/${ productList.categories[0]?.id }` }">{{ productList.categories[0]?.name }}</el-breadcrumb-item> -->
        <el-breadcrumb-item :to="{ path: `/product/${ productList.id }` }">{{ productList.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div
      v-loading="loading"
      element-loading-background="white"
      class="card-container"
    >
      <div class="left-container">
        <!-- 放大镜 -->
        <div class="goods-image">
          <!-- 显示在右侧的放大之后的区域 -->
          <div class="large"
               v-show="!isOutside"
               :style="[{backgroundImage:'url('+productList.mainPictures[index]+')', backgroundPosition: `-${position.x*2}px -${position.y*2}px`}]">
          </div>
          <div class="middle" ref="target">
            <img :src="productList.mainPictures[index]" alt="" />
            <!-- 移动遮罩 -->
            <div class="layer" ref="target" v-show="!isOutside " :style="{ left:position.x+'px', top: position.y+'px' }"></div>
          </div>
        </div>
        <!-- 销量人气 -->
        <div class="product-sales-volume">
          <div class="product-sales-volume-item">
            <div>销量人气</div>
            <span>200+</span>
            <div>
              <el-icon><Flag color="#27ba9b"/></el-icon>
              销量人气
            </div>
          </div>
          <div class="product-sales-volume-item">
            <div>商品评价</div>
            <div>200+</div>
            <div>
              <el-icon><Flag color="#27ba9b"/></el-icon>
              查看评价
            </div>
          </div>
          <div class="product-sales-volume-item">
            <div>收藏人气</div>
            <div>200+</div>
            <div>
              <el-icon><Flag color="#27ba9b"/></el-icon>
              收藏商品
            </div>
          </div>
          <div class="product-sales-volume-item">
            <div>品牌信息</div>
            <div>200+</div>
            <div>
              <el-icon><Flag color="#27ba9b"/></el-icon>
              品牌主页
            </div>
          </div>
        </div>
      </div>
      <!-- 小卡片 -->
      <div class="small-card">
        <div
          v-for="(item, i) in productList.mainPictures"
          :key="item.id"
          @mouseenter="onEnter(i)"
        >
          <img :src="item" alt="">
        </div>
      </div>
      <!-- 商品信息及加入购物车 -->
      <div class="right-container">
        <div>{{ productList.name }}</div>
        <div class="desc">{{ productList.desc }}</div>
        <div>
          <span class="price">{{ indexPrice === -1 ? productList.price : productList.skus[indexPrice].price }}</span>
          <span class="oldPrice">￥{{ indexPrice === -1 ? productList.oldPrice : productList.skus[indexPrice].oldPrice }}</span>
        </div>
        <!-- 促销 配送 服务 -->
        <div class="distribution-container">
          <div>
            <span class="distribution-left">促销</span>
            <span class="distribution-right">12月好物放送,App领券购买直降120元</span>
          </div>
          <div>
            <span class="distribution-left">配送</span>
            <span class="distribution-right">至</span>
            <span>
              <el-cascader
                size="large"
                :options="options"
                v-model="selectedOptions"
                @change="handleChange">
              </el-cascader>
            </span>
          </div>
          <div>
            <span class="distribution-left">服务</span>
            <span class="distribution-right dot">无忧退货</span>
            <span class="distribution-right dot">快速退款</span>
            <span class="distribution-right dot">免费包邮</span>
            <span class="distribution-right learn-more">了解详情</span>
          </div>
        </div>
        <!-- 选择规格 -->
        <!-- 图片类 -->
        <div
          v-show="productList.specs[0]?.values[0]?.picture"
          v-for="(item, i) in productList.specs"
          :key="i"
          class="specification-container"
        >
          <div>{{ item.name }}</div>
          <div
            v-for="(c, idx) in item.values"
            :key="idx"
            :title="c.name"
            v-show="c.picture"
            @click="onSelect(idx)"
            :class="c.picture ? 'specification-img' : ''"
          >
            <img :src="c.picture">
          </div>
          <div
            v-for="(c, idx) in item.values"
            :key="idx"
            v-show="!c.picture"
            class="specification-size"
          >
            <div
              :title="c.name"
              @click="onSelect(idx)"
              class="specification-item"
            >
              {{ c.name }}
            </div>
          </div>

        </div>
        <!-- <div
          v-show="productList.specs[0]?.values[0]?.picture"
          class="specification-container"
        >
          <div>{{ productList.specs[0]?.name }}</div>
          <div
            v-for="(item, i) in productList.specs[0]?.values"
            :key="i"
            :title="item.name"
            @click="onSelect(i)"
            class="specification-img"
          >
            <img :src="item.picture">
          </div>
        </div> -->
        <!-- 文字类 -->
        <div
          v-show="!productList.specs[0]?.values[0]?.picture"
          class="specification-container"
        >
          <div>{{ productList.specs[0]?.name }}</div>
          <div class="specification-size">
            <div
              v-for="(item, i) in productList.specs[0]?.values"
              :key="i"
              @click="onSelect(i)"
              class="specification-item"
            >
              {{ item.name }}
            </div>
          </div>
        </div>
        <div class="specification-container">
          <div>
            数量
          </div>
          <el-input-number v-model="num" :min="1" @change="onNumChange" />
        </div>
        <div>
          <el-button color="#27ba9b">
            <span class="shopping-crat">加入购物车</span>
          </el-button>
        </div>
      </div>
    </div>
    <!-- 轮播图 -->
    <div class="carousel-container">
      <div>
        <div class="header">
          <div>
            同类商品推荐
          </div>
        </div>
        <div class="carousel">
          <el-carousel
            :interval="intervalNum"
            trigger="click"
            arrow="always"
          >
            <el-carousel-item
              v-for="item in subBanner"
              :key="item"
            >
              <div class="carousel-item">
                <div
                  v-for="c in item"
                  :key="c.id"
                  class="card-content"
                >
                  <div>
                    <img class="img" :src="c.picture">
                  </div>
                  <div class="sub-name sub-desc">{{ c.name }}</div>
                  <p>￥{{ c.price }}</p>
                </div>
              </div>
            </el-carousel-item>
          </el-carousel>
        </div>
      </div>
    </div>
    <!-- 产品介绍图 -->
    <div class="product-introduce-container">
      <div class="product-introduce">
        <el-tabs
          v-model="activeName"
          @tab-click="handleClick"
          class="tabs"
        >
          <el-tab-pane label="商品详情" name="first">
            <div class="product-introduce-item">
              <div
                v-for="(item, i) in productList.details.properties"
                :key="i"
              >
                <span class="parameter">{{ item.name }}</span>
                <span class="introduce">{{ item.value }}</span>
              </div>
            </div>
            <div>
              <div
                v-for="(item, i) in productList.details.pictures"
                :key="i"
              >
                <img :src="item" alt="">
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="商品评价" name="second">
            <div class="evaluate"></div>
          </el-tab-pane>
        </el-tabs>
        <div></div>
      </div>
      <div class="hot-commodity">
        <div>
          <div class="hot-list">24小时热销榜</div>
          <div
            v-for="item in subHotSaleList"
            :key="item.id"
            class="card-content"
          >
            <div>
              <img class="img" :src="item.picture">
            </div>
            <div class="sub-name">{{ item.name }}</div>
            <div class="sub-name sub-desc">{{ item.desc }}</div>
            <p>￥{{ item.price }}</p>
          </div>
        </div>
        <div>
          <div class="hot-list">周热销榜</div>
          <div
            v-for="item in subWeekHotList"
            :key="item.id"
            class="card-content"
          >
            <div>
              <img class="img" :src="item.picture">
            </div>
            <div class="sub-name">{{ item.name }}</div>
            <div class="sub-name sub-desc">{{ item.desc }}</div>
            <p>￥{{ item.price }}</p>
          </div>
        </div>
      </div>
    </div>
    <!-- 注意事项 -->
    <div class="attention-container">
      <div class="title">注意事项</div>
      <div class="matter">
        <p class="dot">• 购买运费如何收取？</p>
        <p> 单笔订单金额(不含运费)满88元免邮费；不满88元，每单收取10元运费。（港澳台地区需满500元免邮费；不满500元，每单收取30元运费) </p>
        <p class="dot">• 使用什么快递发货?</p>
        <p>默认使用顺丰快递发货(个别商品使用其他快递）</p>
        <p>配送范围覆盖全国大部分地区(港澳台地区除外）</p>
        <p class="dot">• 如何申请退货?</p>
        <p>1.自收到商品之日起30日内，顾客可申请无忧退货，退款将原路返还，不同的银行处理时间不同，预计1-5个工作日到账； </p>
        <p>2.内裤和食品等特殊商品无质量问题不支持退货；</p>
        <p>3.退货流程： 确认收货-申请退货-客服审核通过-用户寄回商品-仓库签收验货-退款审核-退款完成； </p>
        <p>4.因小兔鲜儿产生的退货，如质量问题，退货邮费由小兔鲜儿承担，退款完成后会以现金券的形式报销。因客户个人原因产生的退货，购买和寄回运费由客户个人承担。 </p>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
  $white: white;
  // 灰色
  $gray: #999999;
.main {
  padding: 0 130px;

  .crumbs {
    // 公共样式面包屑
    @include crumbs();
  }

  // 产品容器
  .card-container {
    display: flex;
    padding: 20px 30px;
    background-color: $white;

    // 左边
    .left-container {
      display: flex;
      flex-direction: column;
      width: 400px;

      // 左边放大镜
      .goods-image {
        width: 400px;
        height: 400px;
        position: relative;
        display: flex;
        z-index: 50;
      .large {
        position: absolute;
        top: 0;
        left: 412px;
        width: 400px;
        height: 400px;
        box-shadow: 0 0 10px rgba(0,0,0,0.1);
        background-repeat: no-repeat;
        // 放大一倍
        background-size: 800px 800px;
        background-color: #f8f8f8;
      }
      .middle {
        width: 400px;
        height: 400px;
        background: #f5f5f5;
        position: relative;
        cursor: move;
        img{
          width: 400px;
          height: 400px;
        }
        .layer {
          width: 200px;
          height: 200px;
          background: rgba(0,0,0,.2);
          left: 0;
          top: 0;
          // 可以移动
          position: absolute;
        }
      }
      .small {
        width: 80px;
        li {
          width: 68px;
          height: 68px;
          margin-left: 12px;
          margin-bottom: 15px;
          cursor: pointer;
          &:hover,
          &.active {
            border: 2px solid red;
          }
        }
      }
      }
      // .left-img {


      //   img {
      //     width: 100%;
      //     height: 100%;
      //     object-fit: cover;
      //   }


      // }


      // 左边的销量
      .product-sales-volume {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        font-size: 14px;
        margin-top: 14px;

        .product-sales-volume-item {
          text-align: center;

          span {
            display: block;
            color: red;
            padding: 8px 0;
          }

          div:nth-child(1) {
            color: $gray;
          }
          div:nth-child(2) {
            color: red;
            padding: 8px 0;
            position: relative;

            &::before {
              content: "";
              position: absolute;
              display: block;
              width: 1px;
              height: 50px;
              background-color: #e4e4e4;
              top: -10px;
            }
          }
          div:nth-child(3) {
            color: #666666;
          }

        }
      }
    }

    // 中间
    .small-card {
      width: 64px;
      height: 400px;
      margin-left: 12px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      &>div {

        img {
          display: block;
          width: 64px;
          height: 64px;
          object-fit: cover;
          border: 1px solid transparent;

          &:hover {
            cursor: pointer;
            border: 1px solid $c;
          }
        }
      }
    }

    // 右边
    .right-container {
      padding-left: 50px;

      &>div {
        margin-top: 14px;
      }

      .desc {
        font-size: 14px;
        color: $gray;
      }

      .price {
        padding-right: 14px;
        color:red
      }

      .oldPrice {
        font-size: 14px;
        color: $gray;
        text-decoration: line-through;
      }
      .distribution-container {
        background-color: #f5f5f5;
        padding: 14px 8px;
        width: 400px;

        div:nth-child(2) {
          padding: 14px 0;
        }
        .distribution-left {
          padding-right: 14px;
          font-size: 14px;
          color: $gray;
        }

        .distribution-right {
          font-size: 14px;
          color: #666666;
        }


        .dot {
          position: relative;
          margin: 0 8px;
          &::before {
            content: "";
            display: inline-block;
            position: absolute;
            top: 50%;
            left: -8px;
            transform: translate(0%, -50%);
            line-height: 14px;
            width: 5px;
            height: 5px;
            border-radius: 50%;
            background-color: $c;
          }
        }


        :deep(.el-input--large .el-input__wrapper) {
          margin-left: 8px;
          height: 20px;
          width: 175px;
        }

        .learn-more {
        color: $c;

        &:hover {
            cursor: pointer;
          }
        }
      }

      .specification-container {
        display: flex;
        align-items: center;

        div:nth-child(1) {
          padding: 0 14px 0 8px;
          font-size: 14px;
          color: $gray;
        }
        .specification-img {
          box-sizing: border-box;
          border: 1px solid #e4e4e4;
          width: 50px;
          height: 50px;
          margin-right: 8px;

          &>img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }

          &:hover {
            cursor: pointer;
            border: 1px solid $c;
          }
        }

        .specification-size {

          .specification-item {
            border: 1px solid #e4e4e4;
            padding: 4px 8px;
            margin-right: 4px;
            color: #666;

            &:hover {
              cursor: pointer;
            }
          }
        }

        :deep(.el-input-number) {
          width: 120px;
        }
      }

      .shopping-crat {
        color: $white;
      }
    }

  }

  // 轮播图
  .carousel-container {
    background-color: $white;
    margin-top: 20px;
    .header {
      height: 80px;
      display: flex;
      align-items: center;

      &>div {
        margin-left: 22px;
      }
    }

    .carousel {

      .carousel-item {
        padding: 0 40px;
        display: flex;
        justify-content: space-around;
        align-items: center;

        .card-content {
          display: flex;
          flex-direction: column;
          align-items: center;
          height: 290px;
          margin-top: 12px;

          img {
            margin-top: 12px;
            width: 100px;
            display: block;
            object-fit: cover;
          }

          .img {
            width: 160px;
            display: block;
            object-fit: cover;
          }

          .sub-name {
            padding-top: 8px;
            text-align: center;
            width: 160px;
            color: #333;
            // 文字省略号
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }

          .sub-desc {
            color: #999999;
            font-size: 14px;
          }

          p {
            color: red
          }

          &:hover {
            color: $c;
            cursor: pointer;
          }
        }
      }

      :deep(.el-carousel__button) {
        // 画小圆点
        border-radius: 50%;
        width: 10px;
        height: 10px;
        background-color: $c;
      }
    }
  }

  // 产品介绍
  .product-introduce-container {
    display: flex;

    .product-introduce {
      flex: 1;

      .tabs {
        background-color: $white;
        margin-top: 22px;

        .product-introduce-item {
          display: grid;
          grid-template-columns: repeat(2, 1fr);
          margin-bottom: 22px;
          padding-left: 20px;

          &>div {
            display: flex;
            .parameter {
              width: 80px;
              display: block;
              color: $gray;
              font-size: 14px;
            }

            .introduce {
              color: #666666;
              font-size: 14px;
            }
          }

        }

        :deep(.el-tabs__nav-scroll) {
          margin-left: 20px;
        }

        // tabs下划线
        :deep(.el-tabs__nav-wrap::after) {
          height: 1px;
        }
        // tabs切换线
        :deep(.el-tabs__active-bar) {
          height: 1px;
        }

        :deep(.el-tabs__nav-scroll) {
          position: relative;

          &::after {
            position: absolute;
            content: "";
            display: block;
            height: 100px;
            width: 1px;
            left: 76px;
            background-color: var(--el-border-color-light);
          }
        }

        .evaluate {
          height: 600px;
        }
      }
    }

    .hot-commodity {
      width: 270px;
      margin-top: 22px;
      margin-left: 10px;

      .hot-list {
        height: 70px;
        width: 100%;
        text-align: center;
        line-height: 70px;
        color: $white;
        background-color: #e26237;
      }

      .card-content {
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-top: 14px;
        background-color: $white;

        img {
          margin-top: 12px;
          width: 100px;
          display: block;
          object-fit: cover;
        }

        .img {
          width: 200px;
          display: block;
          object-fit: cover;
        }

        .sub-name {
          padding-top: 8px;
          text-align: center;
          width: 160px;
          color: #333;
          // 文字省略号
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .sub-desc {
          color: #999999;
          font-size: 14px;
        }

        p {
          color: red
        }

        &:hover {
          color: $c;
          cursor: pointer;
        }
      }
    }
  }

  .attention-container {
    width: 809px;
    margin-top: 22px;
    background-color: $white;

    .title {
      padding-left: 30px;
      color: #333;
      font-size: 18px;
      height: 50px;
      line-height: 50px;
      font-weight: normal;
    }

    .matter {
      padding: 14px;

      p:nth-child(1) {
        margin-top: 0px;
      }

      p {
        font-size: 14px;
        color: #666666;
      }

      .dot {
        color: #333333;
      }
    }
  }

}

</style>
